'''
Author: maolele02
Date: 2022-11-16 16:28:15
LastEditTime: 2023-03-08 20:59:19
LastEditors: maolele02
Description: 
FilePath: \beidou\app.py
'''
from flask import Flask, url_for, redirect, render_template
from flask_jwt_extended import JWTManager
from flask_cors import CORS

from controller.login import LoginView
from controller.user_center import UserCenterView
from controller import user, gaode
from controller import order
from controller import register
from controller import login
from controller import moni_data
from controller import moni_control

from mysql_config import db
import mysql_config

app = Flask(__name__)   # Flask实例化

app.config.from_object(mysql_config)  # 加载数据库配置文件

db.init_app(app)  # 初始化数据库连接

# 由于jinjia与vue在使用{{}}时会产生冲突，故将jinjia的符号进行修改
app.jinja_env.variable_start_string = '{[' # 设定jinja开始的符号（可随意设置）
app.jinja_env.variable_end_string = ']}'  # 设定jinja结束的符号

app.config['JSON_AS_ASCII'] = False  # 不启用ascii来编码json，防止响应乱码

# 全局允许跨域
CORS(app, supports_credentials=True)
# CORS(app, cors_allowed_origins="*")

# Setup the Flask-JWT-Extended extension
app.config["JWT_SECRET_KEY"] = "super-secret"  # Change this!
jwt = JWTManager(app)


# 需要提前自行创建数据库
# create database beidou character set utf8;
with app.app_context():
    db.create_all()  # 创建数据表


@app.route('/login')
def user_login():
    return render_template('index.html')


@app.route('/')
def index():
    return render_template('index.html')


# @app.route('/index')
# def index():
#     render_template('user_view.html')


app.add_url_rule('/login', view_func=LoginView.as_view('login_view'))
app.add_url_rule('/center', view_func=UserCenterView.as_view('user_center_view'))
app.register_blueprint(user.user)
app.register_blueprint(order.order)
app.register_blueprint(register.register)
app.register_blueprint(login.login)
app.register_blueprint(gaode.gaode)
app.register_blueprint(moni_data.moni)
app.register_blueprint(moni_control.ct)

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)


